package com.herench.bs.support;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by luoxiaoming on 2017/8/14.
 */
public class TokenLifeCycle {

    private final String tokenKey;
    private Date expression;
    private final Date connTime;
    private long timeout;
    private boolean alive;

    public TokenLifeCycle(String tokenKey) {
        this.tokenKey = tokenKey;
        Calendar calendar = Calendar.getInstance();
        this.connTime = calendar.getTime();
        calendar.add(Calendar.MILLISECOND, 10 * 60 * 1000);
        this.expression = calendar.getTime();
        timeout = this.expression.getTime() - this.connTime.getTime();
    }

    public String getTokenKey() {
        return tokenKey;
    }

    public Date getExpression() {
        return expression;
    }

    public boolean isAlive() {
        return new Date().compareTo(this.getExpression()) < 0;
    }


    public Date getConnTime() {
        return connTime;
    }

    public long getTimeout() {
        return timeout;
    }

    public void refreshExpression() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MILLISECOND, 10 * 60 * 1000);
        this.expression = calendar.getTime();
    }

}
